package net.dgg.rz.production.controller;

import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.iboss.base.util.http.HttpUtility;
import net.dgg.iboss.base.util.http.ResultObj;
import net.dgg.rz.api.production.service.RecognitionService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.common.utils.SearchUtils;
import net.dgg.rz.production.entity.PayProductRecord;
import net.dgg.rz.production.entity.dto.ChangeSortDto;
import net.dgg.rz.production.entity.dto.UserLevelDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.PayProductRecordService;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.role.entity.Role;
import net.dgg.tmd.foundation.platform.role.service.RoleService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程人员提成管理
 * Created by zyou on 2019/3/29.
 */
@Controller
@RequestMapping("/rzsc/royalty_management")
public class RoyaltyManagementController extends DggBaseController {


    @Autowired
    private RecognitionService recognitionService;

    @Autowired
    private PayProductRecordService payProductRecordService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private SearchUtils searchUtils;
    @Value("${salaryUrl}")
    private String salaryUrl;

    /**
     * 流程人员提成管理
     * @return
     */
    @RequestMapping("/management_index")
    public String managementIndex(Model model){
        UserEntity userEntity = sessionManager.getCurrentUser();
        List<Role> roles = roleService.findUserRoleDTOByUserId(userEntity.getId());
        boolean isChangeLevel = false;
        boolean isChangeSort = false;
        if(roles != null && roles.size()>0){
            for(Role role:roles){
                if(ScProductConstant.CHANGE_LEVEL_BUTTON.equals(role.getCode())){
                    isChangeLevel = true;
                    continue;
                }
                if(ScProductConstant.CHANGE_SORT_BUTTON.equals(role.getCode())){
                    isChangeSort = true;
                    continue;
                }
            }
        }
        model.addAttribute("isChangeLevel",isChangeLevel);
        model.addAttribute("isChangeSort",isChangeSort);
        return "rzsc/royaltymanagement/management_index";
    }
    /**
     * 等级调整
     * @return
     */
    @RequestMapping("/change_level")
    public String changeLevel(Long userId, String curLevel, String curLevelStr, Model model){
        model.addAttribute("userId",userId);
        model.addAttribute("curLevel",curLevel);
        model.addAttribute("curLevelStr",StringUtils.isEmpty(curLevelStr)||"null".equals(curLevelStr)?"":curLevelStr);
        return "rzsc/royaltymanagement/change_level";
    }
    /**
     * 保存等级调整
     * @param
     */
    @RequestMapping(value = "/change_level_save.do")
    @ResponseBody
    public DggRestResponse changeLevelSave(Long userId,String level) {
        try {
            if(userId==null || StringUtils.isEmpty(level)){
                return this.getFailResponse("参数为空！");
            }
            Map map = new HashMap();
            map.put("userId",userId);
            map.put("curLevel",level);
            map.put("businessCode", ScProductConstant.BUS_YT_DK);
            map.put("defaultLevel",ScProductConstant.DEFAULT_LEVEL);
            map.put("rushAccounts",1);
            map.put("type",1);
            String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.CHANGE_LEVEL_SALARY,map);
            ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
            if (resultObj.getCode() == 0) {
                /**
                 * {
                 "doAccountType": "UPDATE",
                 "businessCode": "BUS_YT_DK",
                 "userIds":"userId1,userId2,userId3",
                 "teamIds":"teamId1,teamId2,teamId3",
                 "depIds":"depId1,depId2,depId3",
                 "type": "COMPLETE 全部/APPOINT 部分",
                 "doAccountTime": "2019-04-01,2019-04-30"
                 }
                 */
                //冲账
                Map balanceMap = new HashMap();
                balanceMap.put("doAccountType",SystemCommonConstant.UPDATE);
                balanceMap.put("businessCode",ScProductConstant.BUS_YT_DK);
                balanceMap.put("userIds",userId);
                balanceMap.put("type",SystemCommonConstant.COMPLETE);
                String resBalance = HttpUtility.postJson(salaryUrl + SystemCommonConstant.STRIKE_A_BALANCE,balanceMap);
                ResultObj resBalanceObj = JsonUtil.json2Obj(resBalance, ResultObj.class);
                if (resBalanceObj.getCode() == 0 ) {
                    return this.getSuccessResponse("保存成功");
                }else{
                    return this.getFailResponse(resBalanceObj.getMsg());
                }
            }else{
                return this.getFailResponse(resultObj.getMsg());
            }
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
    /**
     * 等级调整
     * @return
     */
    @RequestMapping("/change_sort")
    public String changeSort(Long userId,String priorityStr, Model model){
        Map map = new HashMap();
        List<Map> sortList = new ArrayList<>();
       try {
           //查询有效的
           map.put("isValid",1);
           String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.GET_SORT_SALARY,map);
           ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
           if (resultObj.getCode() == 0) {
              sortList = JsonUtil.json2List(resultObj.getData(),Map.class);
           }
       }catch (Exception e){
           e.printStackTrace();
       }
        model.addAttribute("sortList",sortList);
        model.addAttribute("userId",userId);
        model.addAttribute("priorityStr",StringUtils.isEmpty(priorityStr)||"null".equals(priorityStr)?"":priorityStr);
        return "rzsc/royaltymanagement/change_sort";
    }
    /**
     * 保存优先级调整
     * @param
     */
    @RequestMapping(value = "/change_sort_save.do")
    @ResponseBody
    public DggRestResponse changeSortSave(ChangeSortDto changeSortDto) {
        try {
            DggValidateUtil.notNull(changeSortDto,ProductOrderExcption.class,"参数为空！");
            DggValidateUtil.notNull(changeSortDto.getUserId(),ProductOrderExcption.class,"用户为空！");
            //DggValidateUtil.notNull(changeSortDto.getSortArr(),ProductOrderExcption.class,"优先级为空！");
            Map map = new HashMap();
            map.put("userId",changeSortDto.getUserId());
            map.put("priority", JSONObject.toJSONString(changeSortDto.getSortArr()));
            map.put("businessCode", ScProductConstant.BUS_YT_DK);
            map.put("defaultLevel",ScProductConstant.DEFAULT_LEVEL);
            map.put("type",2);
            String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.CHANGE_LEVEL_SALARY,map);
            ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
            if (resultObj.getCode() == 0) {
                return this.getSuccessResponse("保存成功");
            }else{
                return this.getFailResponse(resultObj.getMsg());
            }
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
    /**
     * 查询流程人员提成管理
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/management_list")
    @ResponseBody
    public DggRestResponse managementList(@RequestParam Map params) {
        try {
            return new DataTableResponse().data(params,this.queryManagementList(params));
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    private List<UserLevelDto> queryManagementList(Map params){
        //根据当前登陆人查询当前部门及以下的流程人员
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(userEntity.getOrgId()==null){
            return null;
        }else{
            List<UserLevelDto> ruleDetails = recognitionService.managementListWithPage(params,userEntity.getOrgId());
            if(ruleDetails != null && ruleDetails.size()>0){
                List<Map> pullList =  new ArrayList<>();
                Map pullParam = null;
                for(UserLevelDto userLevelDto:ruleDetails){
                    pullParam = new HashMap();
                       /* pullParam.put("defaultLevel",ScProductConstant.DEFAULT_LEVEL);
                        pullParam.put("hasMonitor",ScProductConstant.IS_HAS_MONITOR);*/
                    pullParam.put("userId",userLevelDto.getUserId());
                    pullParam.put("businessCode",ScProductConstant.BUS_YT_DK);
                    pullList.add(pullParam);
                }
                //去薪酬系统查询人员级别等信息
                Map map = new HashedMap();
                map.put("userList",pullList);
                try {
                    String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.GET_USER_LEVEL_SALARY,map);
                    ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
                    if (resultObj.getCode() == 0) {
                        String resData = resultObj.getData();
                        List<UserLevelDto> userLevelDtosRes = JsonUtil.json2List(resData,UserLevelDto.class);
                        for(UserLevelDto userLevelDto:ruleDetails){
                            for(UserLevelDto userLevelDtoRes:userLevelDtosRes){
                                if(userLevelDtoRes.getUserId() !=null && userLevelDto.getUserId().longValue()==userLevelDtoRes.getUserId().longValue()){
                                    userLevelDto.setCurLevel(userLevelDtoRes.getCurLevel());
                                    userLevelDto.setCurLevelStr(translateUtil.translateCode(userLevelDtoRes.getCurLevel()));
                                    userLevelDto.setOldLevel(translateUtil.translateCode(userLevelDtoRes.getOldLevel()));
                                    userLevelDto.setPriorityStr(userLevelDtoRes.getPriority());
                                    continue;
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            //翻译字段
              /*  ruleDetails.parallelStream().forEach(temp->{
                    temp.setCurLevelStr(translateUtil.translateCode(temp.getCurLevel()));
                    temp.setOldLevel(translateUtil.translateCode(temp.getOldLevel()));
                });*/
            return ruleDetails;
        }
    }

    /**
     * 流程人员我的提成
     * @return
     */
    @RequestMapping("/my_royalty")
    public String myRoyalty(Model model){
        model.addAttribute("curDate",SearchUtils.getMonthDay());
        return "rzsc/royaltymanagement/my_royalty";
    }

    /**
     * 查询我的提成
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/royalty_list")
    @ResponseBody
    public DggRestResponse royaltyList(@RequestParam Map params) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            params.put("userId",userEntity.getId());
            List<PayProductRecord> payProductRecords = payProductRecordService.royaltyListWithPage(params);
            return new DataTableResponse().data(params,payProductRecords);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 流程人员部门提成
     * @return
     */
    @RequestMapping("/dept_royalty")
    public String deptRoyalty(){
        return "rzsc/royaltymanagement/dept_royalty";
    }
    /**
     * 流程人员部门提成汇总页面
     * @return
     */
    @RequestMapping("/dept_all")
    public String deptAll(Model model){
        model.addAttribute("curDate",SearchUtils.getMonthDay());
        return "rzsc/royaltymanagement/dept_all";
    }
    /**
     * 流程人员部门提成明细页面
     * @return
     */
    @RequestMapping("/dept_detail")
    public String deptDetail(Model model){
        model.addAttribute("curDate",SearchUtils.getMonthDay());
        return "rzsc/royaltymanagement/dept_detail";
    }

    /**
     * 查询流程人员部门提成汇总
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/dept_all_list")
    @ResponseBody
    public DggRestResponse deptAllList(@RequestParam Map params) {
        try {
            //查询部门提成汇总
            List<UserLevelDto> ruleDetails = this.queryDeptAll(params);
            return new DataTableResponse().data(params,ruleDetails);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     *
     * @param params
     * @return
     */
    private List<UserLevelDto> queryDeptAll(Map params) {
        //根据当前登陆人查询当前部门及以下的流程人员
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(userEntity.getOrgId()==null){
            return null;
        }else{
            List<UserLevelDto> ruleDetails = recognitionService.managementListWithPage(params,userEntity.getOrgId());
            if(ruleDetails != null && ruleDetails.size()>0){
                List<Long> pullList =  new ArrayList<>();
                for(UserLevelDto userLevelDto:ruleDetails){
                    pullList.add(userLevelDto.getUserId());
                }
                //去薪酬系统查询人员级别等信息
                Map map = new HashedMap();
                map.put("userList",pullList);
                map.put("orgIds",params.get("orgIds"));
                map.put("royaltyTime",params.get("royaltyTime"));
                map.put("businessCode",ScProductConstant.BUS_YT_DK);
                searchUtils.paramsRoyalty(map);
                try {
                    String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.GET_DEPT_ALL_SALARY,map);
                    ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
                    if (resultObj.getCode() == 0) {
                        String resData = resultObj.getData();
                        List<JSONObject> userLevelDtosRes = JsonUtil.json2List(resData,JSONObject.class);
                        for(UserLevelDto userLevelDto:ruleDetails){
                            for(JSONObject userLevelDtoRes:userLevelDtosRes){
                                if(userLevelDtoRes.getLong("userId") != null && userLevelDto.getUserId().longValue()==userLevelDtoRes.getLong("userId").longValue()){
                                    userLevelDto.setRetJson(userLevelDtoRes);
                                    continue;
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            return ruleDetails;
        }
    }


    /**
     * 查询流程人员部门提成明细
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/dept_detail_list")
    @ResponseBody
    public DggRestResponse deptDetailList(@RequestParam Map params) {
        try {
            List<PayProductRecord> payProductRecords = this.queryDeptDetail(params);
            return new DataTableResponse().data(params,payProductRecords);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    private List<PayProductRecord> queryDeptDetail(Map params){
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(userEntity.getOrgId()==null){
            return null;
        }else{
            List<UserLevelDto> ruleDetails = recognitionService.managementList(params,userEntity.getOrgId());
            if(ruleDetails != null && ruleDetails.size()>0){
                List<Map> pullList =  new ArrayList<>();
                List<Long> userList =  new ArrayList<>();
                List<Long> userOrgs =  new ArrayList<>();
                Map pullParam = null;
                for(UserLevelDto userLevelDto:ruleDetails){
                    pullParam = new HashMap();
                    pullParam.put("userId",userLevelDto.getUserId());
                    pullParam.put("businessCode",ScProductConstant.BUS_YT_DK);
                    userList.add(userLevelDto.getUserId());
                    userOrgs.add(userLevelDto.getOrgId());
                    pullList.add(pullParam);
                }
                //去薪酬系统查询人员级别等信息
                Map map = new HashedMap();
                map.put("userList",pullList);
                params.put("userList",userList);
                List<PayProductRecord> payProductRecords = payProductRecordService.royaltyListWithPage(params);
                return payProductRecords;
            }
            return new ArrayList<>();
        }
    }


    /**
     * 导出(我的提成)
     */
    @RequestMapping(value = "excel_my_royalty")
    @ResponseBody
    public void excelExportMyRoyalty(@RequestParam Map params) {
        int[] widths = new int[] { 512*8,512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1,-1,-1, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0};
        UserEntity userEntity = sessionManager.getCurrentUser();
        params.put("userId",userEntity.getId());
        List<PayProductRecord> payProductRecords = payProductRecordService.royaltyListWithPage(params);
        //设置提成金额为小数点后2位  四舍五入
        for (int i = 0; i < payProductRecords.size(); i++) {
            PayProductRecord payProductRecord = payProductRecords.get(i);
            BigDecimal performanceData = payProductRecord.getPerformanceData();
            BigDecimal bigDecimal = performanceData.setScale(2, BigDecimal.ROUND_HALF_UP);
            payProductRecord.setPerformanceData(bigDecimal);
        }
        String sheetName  = "我的提成";
        String[] title = null;
        String[] keyArray = null;

        title = new String[]{"流程人员","生产事业部","部门","生产单编号","产品","商品","渠道","结项内部成本（元）","自开发利润金额（元）","返点利润金额（元）","业绩提成比例","自开发提成比例","提成金额（元）"};
        keyArray = new String[] {"payUserName","productOrderOrgName","productOrderUserOrgName", "productOrderNo","productName","commodityName","channelName","calculationItem.BUS_YT_DK_nbcb","calculationItem.BUS_YT_DK_zkflr","calculationItem.BUS_YT_DK_fdlr","calculationItem.BUS_YT_DK_yjtcxs","calculationItem.BUS_YT_DK_zkftcxs","performanceData"};
        ExcelTool.execlExportJson(payProductRecords,sheetName,title,keyArray,widths,styleArr);
    }

    /**
     * 导出(部门提成汇总)
     */
    @RequestMapping(value = "excel_dept_all")
    @ResponseBody
    public void excelDeptAll(@RequestParam Map params) {
        int[] widths = new int[] { 512*8,512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1,-1,-1, 0, 0 , 0, 0, 0};
        //查询部门提成汇总
        List<UserLevelDto> ruleDetails = this.queryDeptAll(params);
        String sheetName  = "部门提成汇总";
        String[] title = null;
        String[] keyArray = null;

        title = new String[]{"流程人员","工号","部门","提成总笔数","结项内部成本（元）","自开发利润（元）","返点利润（元）","提成总金额（元）"};
        keyArray = new String[] {"realName","loginName","orgName", "retJson.amount","retJson.BUS_YT_DK_nbcb","retJson.BUS_YT_DK_zkflr","retJson.BUS_YT_DK_fdlr","retJson.performance"};
        ExcelTool.execlExportJson(ruleDetails,sheetName,title,keyArray,widths,styleArr);
    }

    /**
     * 导出(部门提成明细)
     */
    @RequestMapping(value = "excel_dept_detail")
    @ResponseBody
    public void excelDeptDetail(@RequestParam Map params) {
        int[] widths = new int[] { 512*8,512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1,-1,-1, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0};
        List<PayProductRecord> payProductRecords = this.queryDeptDetail(params);
        String sheetName  = "部门提成明细";
        String[] title = null;
        String[] keyArray = null;

        title = new String[]{"流程人员","生产事业部","部门","生产单编号","产品","商品","渠道","结项内部成本（元）","自开发利润金额（元）","返点利润金额（元）","业绩提成比例","自开发提成比例","提成金额（元）"};
        keyArray = new String[] {"payUserName","productOrderOrgName","productOrderUserOrgName", "productOrderNo","productName","commodityName","channelName","calculationItem.BUS_YT_DK_nbcb","calculationItem.BUS_YT_DK_zkflr","calculationItem.BUS_YT_DK_fdlr","calculationItem.BUS_YT_DK_yjtcxs","calculationItem.BUS_YT_DK_zkftcxs","performanceData"};
        ExcelTool.execlExportJson(payProductRecords,sheetName,title,keyArray,widths,styleArr);
    }
    /**
     * 导出(流程人员提成管理)
     */
    @RequestMapping(value = "excel_management_index")
    @ResponseBody
    public void excelManagementIndex(@RequestParam Map params) {
        int[] widths = new int[] { 512*8,512*8,512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1,-1, 0, 0 , 0, 0};
        List<UserLevelDto> userLevelDtos = this.queryManagementList(params);
        String sheetName  = "部门提成明细";
        String[] title = null;
        String[] keyArray = null;

        title = new String[]{"姓名","工号","部门","当前级别","上次级别","优先级"};
        keyArray = new String[] {"realName","loginName","orgName", "curLevelStr","oldLevel","priorityStr"};
        ExcelTool.execlExportJson(userLevelDtos,sheetName,title,keyArray,widths,styleArr);
    }


    /**
     * 我的提成 总业绩
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/get_total_royalty_my")
    @ResponseBody
    public DggRestResponse getTotalRoyaltyMy(@RequestParam Map params) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            List<Long> userList = new ArrayList<>();
            userList.add(userEntity.getId());
            params.put("userList",userList);
            params.put("businessCode",ScProductConstant.BUS_YT_DK);
            params.put("queryField",SystemCommonConstant.ACHIEVEMENT);
            searchUtils.paramsRoyalty(params);
            String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.FIND_USER_TOTAL,params);
            ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
            JSONObject retJson = null;
            if (resultObj.getCode() == 0) {
                String resData = resultObj.getData();
                if(!StringUtils.isEmpty(resData)){
                    retJson = JSONObject.parseObject(resData);
                }
            }
            return this.getSuccessResponse(retJson);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 部门提成汇总 总业绩
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/get_total_dept_all")
    @ResponseBody
    public DggRestResponse getTotalDeptAll(@RequestParam Map params) {
        try {
            List<UserLevelDto> userLevelDtos = this.queryDeptAll(params);
            List<Long> userList = new ArrayList<>();
            if(userLevelDtos != null && userLevelDtos.size()>0){
                for (UserLevelDto u : userLevelDtos) {
                    userList.add(u.getUserId());
                }
            }
            params.put("userList",userList);
            params.put("businessCode",ScProductConstant.BUS_YT_DK);
            params.put("queryField",SystemCommonConstant.ACHIEVEMENT);
            searchUtils.paramsRoyalty(params);
            String bas = HttpUtility.postJson(salaryUrl + SystemCommonConstant.FIND_USER_TOTAL,params);
            ResultObj resultObj = JsonUtil.json2Obj(bas, ResultObj.class);
            JSONObject retJson = null;
            if (resultObj.getCode() == 0) {
                String resData = resultObj.getData();
                if(!StringUtils.isEmpty(resData)){
                    retJson = JSONObject.parseObject(resData);
                }
            }
            return this.getSuccessResponse(retJson);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
}
